package com.ct.application.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.application.system.model.vo.AdminSystemLoginVO;
import com.ct.application.system.model.vo.AdminSystemVO;
import com.ct.application.system.service.IAdminSystemUserService;
import com.ct.system.mapper.AdminSystemMapper;
import com.ct.system.orm.AdminSystemUser;
import com.ct.system.responsity.admin.IAdminSystemUserRepository;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-16
 */
@Service
public class AdminSystemUserService  implements IAdminSystemUserService {


    @Resource
    private AdminSystemMapper adminSystemMapper;
    @Resource
    private IAdminSystemUserRepository adminSystemUserRepository;

    @Override
    public List<AdminSystemVO> findAdminSystemAll(Long userId) {
        return adminSystemUserRepository.tranferListBo(adminSystemMapper.findAdminSystemByUserId(userId),AdminSystemVO.class);
    }
    @Override
    public List<AdminSystemLoginVO> findAdminSystemLoginAll(Long userId) {
        return adminSystemUserRepository.tranferListBo(adminSystemMapper.findAdminSystemByUserId(userId),AdminSystemLoginVO.class);
    }

    @Override
    public boolean unbindSystemByUserId(Long userId,Long systemId) {
        LambdaQueryWrapper<AdminSystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemUser::getUserId,userId);
        lambdaQueryWrapper.eq(AdminSystemUser::getSystemId,systemId);
        return adminSystemUserRepository.remove(lambdaQueryWrapper);
    }

    @Override
    public boolean bindSystemByUserId(Long userId, Long systemId) {
        LambdaQueryWrapper<AdminSystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSystemUser::getUserId,userId);
        lambdaQueryWrapper.eq(AdminSystemUser::getSystemId,systemId);
        long count = adminSystemUserRepository.count(lambdaQueryWrapper);
        if(count == 0){
            AdminSystemUser adminSystemUser = new AdminSystemUser();
            adminSystemUser.setUserId(userId);
            adminSystemUser.setSystemId(systemId);
            adminSystemUserRepository.saveOrUpdate(adminSystemUser);
            return true;
        }
        return false;
    }
}
